package com.aiti.lulian.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.aiti.base.core.exception.BusinessException;
import com.aiti.lulian.entity.LuLianEnterprise;
import com.aiti.lulian.entity.LuLianEnterpriseExtension;
import com.aiti.lulian.entity.LuLianEnterpriseHistory;
import com.aiti.lulian.entity.LuLianEnterpriseUser;
import com.aiti.lulian.mapper.LuLianEnterpriseHistoryMapper;
import com.aiti.lulian.mapper.LuLianEnterpriseMapper;
import com.aiti.lulian.mapper.LuLianEnterpriseUserMapper;
import com.aiti.lulian.service.ILuLianEnterpriseExtensionService;
import com.aiti.lulian.service.ILulianIndexService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class ILulianIndexServiceImpl implements ILulianIndexService {

    private LuLianEnterpriseHistoryMapper luLianEnterpriseHistoryMapper;

    private LuLianEnterpriseMapper luLianEnterpriseMapper;

    private LuLianEnterpriseUserMapper luLianEnterpriseUserMapper;

    private ILuLianEnterpriseExtensionService luLianEnterpriseExtensionService;

    public ILulianIndexServiceImpl(LuLianEnterpriseHistoryMapper luLianEnterpriseHistoryMapper, LuLianEnterpriseMapper luLianEnterpriseMapper, LuLianEnterpriseUserMapper luLianEnterpriseUserMapper, ILuLianEnterpriseExtensionService luLianEnterpriseExtensionService) {
        this.luLianEnterpriseHistoryMapper = luLianEnterpriseHistoryMapper;
        this.luLianEnterpriseMapper = luLianEnterpriseMapper;
        this.luLianEnterpriseUserMapper = luLianEnterpriseUserMapper;
        this.luLianEnterpriseExtensionService = luLianEnterpriseExtensionService;
    }

    /**
     * 查询当前登陆人的企业名称
     *
     * @param userId
     * @return
     */
    @Override
    public String getIndexCompanyName(String userId) {

        //查询用户绑定的企业
        LuLianEnterpriseUser enterpriseUser = luLianEnterpriseUserMapper.selectOne(new QueryWrapper<LuLianEnterpriseUser>().eq("user_id", userId));
        if(enterpriseUser == null){
            return "";
        }
        LuLianEnterpriseHistory luLianEnterpriseHistory = luLianEnterpriseHistoryMapper.selectOne(new QueryWrapper<LuLianEnterpriseHistory>().eq("base_id", enterpriseUser.getEnterpriseId()).eq("status", "1"));
        if(luLianEnterpriseHistory == null){
            return "";
        }

        return luLianEnterpriseHistory.getBaseName();
    }

    @Override
    public Map<String, Object> getCompanyInformation(String userId) {
        Map<String, Object> map = new HashMap<>();
        //企业ID
        String companyId = "";
        //企业名称
        String companyName = "";
        //主营产品
        String mainProducts = "";
        //LOGO
        String logo = "";
        //默认的名片背景布局编号
        String layoutNum = "";
        //是否允许员工修改转发背景
        String isShare = "0";
        //默认的员工转发背景样式
        String pubBackgroundStyle = "";
        //默认的员工转发背景URL
        String pubBackgroundUrl = "";
        //转发标题
        String shareTitle = "";
        //转发标题
        String shareTextColor = "";
        // 公司电话
        String companyTel = "";
        // “山东制造·云上展厅”资质认定
        Boolean sdzzYszt = false;
        // “山东制造·品牌认定”资质认定
        Boolean sdzzPprd = false;
        //查询用户绑定的企业
        List<LuLianEnterpriseUser> enterpriseUserList = luLianEnterpriseUserMapper.selectList(new LambdaQueryWrapper<LuLianEnterpriseUser>().eq(LuLianEnterpriseUser::getUserId, userId));
        if (enterpriseUserList != null && enterpriseUserList.size() > 1) {
            //当用户关联了多个企业时
            log.error("==========> ID为【{}】的用户关联了多家企业，请及时处理！ <=========", userId);
            throw new RuntimeException("该账号关联了多家企业，请联系平台工作人员进行处理");
        }
        if(enterpriseUserList == null || enterpriseUserList.size() == 0) {
            //当用户没有关联企业时
            map.put("companyId", companyId);
            map.put("companyName", companyName);
            map.put("mainProducts", mainProducts);
            map.put("logo", logo);
            map.put("layoutNum", layoutNum);
            map.put("isShare", isShare);
            map.put("pubBackgroundStyle", pubBackgroundStyle);
            map.put("pubBackgroundUrl", pubBackgroundUrl);
            map.put("shareTitle", shareTitle);
            map.put("shareTextColor", shareTextColor);
            map.put("companyTel", companyTel);
            map.put("sdzzYszt", sdzzYszt);
            map.put("sdzzPprd", sdzzPprd);
        } else {
            //当用户有且仅关联了一个企业时
            List<LuLianEnterprise> luLianEnterpriseList = luLianEnterpriseMapper.selectList(new LambdaQueryWrapper<LuLianEnterprise>().eq(LuLianEnterprise::getBaseId, enterpriseUserList.get(0).getEnterpriseId()).eq(LuLianEnterprise::getStatus, "1"));
            if (luLianEnterpriseList != null && luLianEnterpriseList.size() > 1) {
                log.error("=========> ID为【{}】的企业数据重复，请及时处理！ <=========", enterpriseUserList.get(0).getEnterpriseId());
                throw new RuntimeException("企业数据重复，请联系平台工作人员进行处理");
            }
            if(luLianEnterpriseList == null || luLianEnterpriseList.size() == 0) {
                map.put("companyId", companyId);
                map.put("companyName", companyName);
                map.put("mainProducts", mainProducts);
                map.put("logo", logo);
                map.put("layoutNum", layoutNum);
                map.put("isShare", isShare);
                map.put("pubBackgroundStyle", pubBackgroundStyle);
                map.put("pubBackgroundUrl", pubBackgroundUrl);
                map.put("shareTitle", shareTitle);
                map.put("shareTextColor", shareTextColor);
                map.put("companyTel", companyTel);
                map.put("sdzzYszt", sdzzYszt);
                map.put("sdzzPprd", sdzzPprd);
            } else {
                LuLianEnterprise luLianEnterprise = luLianEnterpriseList.get(0);
                map.put("companyId", luLianEnterprise.getBaseId());
                map.put("companyName", luLianEnterprise.getBaseName());
                map.put("mainProducts", StrUtil.isEmpty(luLianEnterprise.getMainProducts()) ? "" : luLianEnterprise.getMainProducts());
                map.put("logo", StrUtil.isEmpty(luLianEnterprise.getLogo()) ? "" : luLianEnterprise.getLogo());
                LuLianEnterpriseExtension enterpriseExtension = luLianEnterpriseExtensionService.getOne(new LambdaQueryWrapper<LuLianEnterpriseExtension>().eq(LuLianEnterpriseExtension::getEnterpriseId, luLianEnterprise.getBaseId()));
                if (enterpriseExtension != null) {
                    layoutNum = enterpriseExtension.getLayoutNum();
                    isShare = enterpriseExtension.getIsShare();
                    pubBackgroundStyle = enterpriseExtension.getPubBackgroundStyle();
                    pubBackgroundUrl = enterpriseExtension.getPubBackgroundUrl();
                    shareTitle = enterpriseExtension.getShareTitle();
                    shareTextColor = enterpriseExtension.getShareTextColor();
                }
                map.put("layoutNum", StrUtil.isEmpty(layoutNum) ? "" : layoutNum);
                map.put("isShare", StrUtil.isEmpty(isShare) ? "0" : isShare);
                map.put("pubBackgroundStyle", StrUtil.isEmpty(pubBackgroundStyle) ? "" : pubBackgroundStyle);
                map.put("pubBackgroundUrl", StrUtil.isEmpty(pubBackgroundUrl) ? "" : pubBackgroundUrl);
                map.put("shareTitle", shareTitle);
                map.put("shareTextColor", shareTextColor);
                map.put("companyTel", luLianEnterprise.getCompanyTel());
                map.put("sdzzYszt", luLianEnterprise.getSdzzYszt());
                map.put("sdzzPprd", luLianEnterprise.getSdzzPprd());
            }
        }
        return map;
    }

    public Map<String, Object> getCeCompanyInformation(String userId) {
        List<LuLianEnterpriseUser> enterpriseUserList = luLianEnterpriseUserMapper.selectList(new LambdaQueryWrapper<LuLianEnterpriseUser>().eq(LuLianEnterpriseUser::getUserId, userId));
        if (enterpriseUserList != null && enterpriseUserList.size() > 1) {
            //当用户关联了多个企业时
            log.error("==========> ID为【{}】的用户关联了多家企业，请及时处理！ <=========", userId);
            throw new BusinessException("该账号关联了多家企业，请联系平台工作人员进行处理");
        }
        Map<String, Object> map = new HashMap<>();
        if(CollectionUtil.isEmpty(enterpriseUserList)) {
            map.put("companyId", "");
            map.put("companyName", "");
            return map;
        }
        LuLianEnterpriseHistory enterpriseHistory = luLianEnterpriseHistoryMapper.selectOne(new LambdaQueryWrapper<LuLianEnterpriseHistory>()
                .eq(LuLianEnterpriseHistory::getBaseId, enterpriseUserList.get(0).getEnterpriseId())
                .eq(LuLianEnterpriseHistory::getStatus, "1")
                .eq(LuLianEnterpriseHistory::getIsDelete, "0"));
        if(enterpriseHistory == null) {
            throw new BusinessException("未查询到该用户绑定的企业数据，请联系平台工作人员进行处理");
        }
        map.put("companyId", enterpriseHistory.getBaseId());
        map.put("companyName", enterpriseHistory.getBaseName());
        return map;
    }
}